var fs = require('fs')
var obj = {
  a: 111,
  then: function () {
    console.log(this.a)
  }
}
Promise.resolve(obj) // 111

var p = Promise.resolve('Hello World')
p.then(res => console.log(res)) // Hello World

Promise.reject('Hello World')
  .catch(err => {
    console.log('err_', err) // reject 要用 catch 捕获，否则可能使得进程退出
  })


function readFile_promise(path) {
  return new Promise((resolve, reject) => {
    fs.readFile(path, (err, data) => {
      if (err) {
        reject(err)
      } else {
        resolve(data)
      }
    })
  })
}

Promise.all([readFile_promise('../foo.txt'), readFile_promise('../baz.txt'), readFile_promise('../bar.txt')])
        .then(res => {
          console.log(res)
        })
        .catch(err => console.log(err))

Promise.race([readFile_promise('../baz.txt'), readFile_promise('../bar.txt')])
        .then(res => {
          console.log('race________', res.toString())
        })
        .catch(err => console.log('err____', err))

var promise = new Promise((resolve, reject) => {
  // throw new Error('get error')
  reject(new Error('get err'))
})
promise.catch(err => console.log(err))